package control.aspect;

import base.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * Log aspect
 *
 * @description: 日志切面
 * @author: cyj
 * @date 2020 -10-15 17:18:15
 */
@Aspect
@Component
@Slf4j
@Order(0)
public class LogAspect {

    /**
     * Web log 切入点
     */
    @Pointcut("execution(* control.control..*.* (..))")
    public void webLog(){}

    /**
     * Do before 调用前日志记录
     *
     * @param joinPoint join point
     * @throws Throwable throwable
     */
    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        Method method = BeanUtil.getMethod(joinPoint);
        Object[] args = joinPoint.getArgs();
        StringBuffer argsBuffer = new StringBuffer();
        Arrays.asList(args).stream().forEach(argsBuffer::append);
        log.info("begin call method: [{}], args: [{}]", method.getName(), argsBuffer);
    }

    /**
     * Do after 后调用日志
     *
     * @param joinPoint join point
     * @throws Throwable throwable
     */
    @After("webLog()")
    public void doAfter(JoinPoint joinPoint) throws Throwable {
        Method method = BeanUtil.getMethod(joinPoint);
        log.info("end call method: [{}]", method.getName());
    }
}
